package com.common.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.log4j.Logger;

import com.common.util.BeanUtils;
import com.common.util.StringUtil;

public class SelectTag extends BaseTag {
	private Logger log = Logger.getLogger(this.getClass());
	private String id;
	private String name;
	private String value;

	private String preId="";
	private String preName="";
	
	private String beanName;
	
	private Object objList;
	private String objText;
	private String objKey;


	private String onClick="";
	public String getOnClick() {
		return onClick;
	}


	public void setOnClick(String onClick) {
		this.onClick = onClick;
	}


	public String getObjKey() {
		return objKey;
	}


	public void setObjKey(String objKey) {
		this.objKey = objKey;
	}


	public Object getObjList() {
		return objList;
	}


	public void setObjList(Object objList) {
		this.objList = objList;
	}


	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		 StringBuilder outStr = new StringBuilder();
		 if(id==null){
			 id=name;
		 }
		 outStr.append("<select name =\""+name+"\" id=\""+id+"\"");
		 
		 if(!StringUtil.isBlank(onClick)){
			 outStr.append(" onchange='"+onClick+"'");
		 }
		 outStr.append(">");
		 if(!StringUtil.isBlank(preName)){
			 outStr.append("<option value='"+preId+"'");
			 if(preId!=null&&preId.equals(value)){
				 outStr.append(" selected='selected'");
				 
			 }
			 outStr.append(">"+preName+"</option>");
		 }
		 if(objList!=null){
			 List list = (List) objList;
			 for (int i = 0; i <list.size(); i++) {
				Object obj = list.get(i);
				String key = (String) BeanUtils.forceGetProperty(obj, objKey);
				String text = (String) BeanUtils.forceGetProperty(obj, objText);
				outStr.append("<option value='"+key+"'");
				 if(key!=null&&key.equals(value)){
					 outStr.append(" selected='selected'");
				 }
				 outStr.append(">"+text+"</option>");
			}
			 
		 }else{
			 if(!StringUtil.isBlank(beanName)){
			 Map<String,String> beanMap = (Map<String,String>) getServletContext().getAttribute(beanName);
			
			 if(beanMap!=null){
			 Iterator keyiter = beanMap.keySet().iterator();
			 while(keyiter.hasNext()){
				 String key = (String) keyiter.next();
				 String text = (String) beanMap.get(key);
				 outStr.append("<option value='"+key+"'");
				 if(key!=null&&key.equals(value)){
					 outStr.append(" selected='selected'");
				 }
				 outStr.append(">"+text+"</option>");
			 }
			 } 	
			 }
		 }
		 outStr.append("</select>");
		log.info("outStr====="+outStr);
		try {
			out.print(outStr);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return super.doStartTag();
	}


	public String getBeanName() {
		return beanName;
	}


	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getValue() {
		return value;
	}


	public void setValue(String value) {
		this.value = value;
	}
	public String getPreId() {
		return preId;
	}


	public void setPreId(String preId) {
		this.preId = preId;
	}


	public String getPreName() {
		return preName;
	}


	public void setPreName(String preName) {
		this.preName = preName;
	}


	public String getObjText() {
		return objText;
	}


	public void setObjText(String objText) {
		this.objText = objText;
	}


}
